<?php

namespace stlswm\DouyinOpenApi\mp;

use stlswm\DouyinOpenApi\CurlHttp;
use stlswm\DouyinOpenApi\Response;

class Login
{
    use CurlHttp;

    private static string $url     = 'https://developer.toutiao.com/api/apps/v2/jscode2session';
    private static string $urlTest = 'https://open-sandbox.douyin.com/api/apps/v2/jscode2session';

    public string $appid;
    public string $code;
    public string $secret;
    public string $anonymous_code;

    /**
     * @param  array  $body       请求体
     * @param  bool   $inSandBox  是否沙盒测试环境
     * @return Response
     */
    public function send(array &$body = [], bool $inSandBox = false): Response
    {
        $allowKeys = [
            'appid',
            'code',
            'secret',
            'anonymous_code',
        ];
        foreach ($allowKeys as $key) {
            if (isset($this->$key)) {
                $body[$key] = $this->$key;
            }
        }

        return self::postJson($inSandBox ? self::$urlTest : self::$url, $body);
    }
}